//
//  CustomView.m
//  UIBezierPath_Demo
//
//  Created by LaoYue on 2023/8/3.
//

#import "CustomView.h"

@implementation CustomView

- (void)drawRect:(CGRect)rect
{
    [[UIColor redColor] set];
    
    {// 1. 直线
        UIBezierPath *path = [[UIBezierPath alloc] init]; //初始化也可以用 [UIBezierPath bezierPath];
        path.lineWidth = 3.f;
        path.lineCapStyle = kCGLineCapRound; //端点样式（圆形）
        path.lineJoinStyle = kCGLineJoinRound; //连接类型
        path.miterLimit = 10.f;
        path.flatness = 10.f; //绘线的精细程度，默认为0.6，数值越大，需要处理的时间越长
        path.usesEvenOddFillRule = YES;
        
        // 设置起始点
        [path moveToPoint:CGPointMake(30, 30)];
        
        // 添加子路径
        [path addLineToPoint:CGPointMake(100, 20)];//添加一条子路径
        [path addLineToPoint:CGPointMake(200, 50)];//添加一条子路径
        [path closePath]; //当构建子路径数>=2条时,可以调用`closePath`方法来闭合路径.
        
        // 根据坐标点连线
        [path stroke]; //各个点连线
    }
    
    {// 2.矩形，可以用3个 addLineToPoint: 方法，也可以直接用下面方法：
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 70, 50, 50)];
        [path fill]; //设置填充
        [path stroke];
    }
    
    
    {// 3.圆角矩形
        UIBezierPath *path1 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(120, 70, 50, 50) cornerRadius:10];
        [path1 stroke];
        
        UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(220, 70, 50, 50)
                                                    byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomRight
                                                            cornerRadii:CGSizeMake(10, 10)];
        [path2 stroke];
    }
    
    {// 4.椭圆
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 140, 100, 50)];
        [path stroke];
    }

    {// 5.圆弧
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(20, 200)
                                                            radius:50
                                                        startAngle:0
                                                          endAngle:M_PI/2
                                                         clockwise:YES];
        [path fill];
        [path stroke];
    }
    
    {// 6. 扇形
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(100, 260)];
        [path addArcWithCenter:CGPointMake(200, 260) radius:50 startAngle:0 endAngle:M_PI/2 clockwise:YES];
        [path closePath]; //形成闭环
        [path stroke];
    }
    
    {// 7.虚线
        UIBezierPath *path = [UIBezierPath bezierPath];
        //UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 200)]; //椭圆虚线
        //UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 100, 100) cornerRadius:40]; //圆虚线
        CGFloat dash[] = {3.0, 3.0};
        [path setLineDash:dash //各段虚线长度
                    count:2 //数组数量
                    phase:0.0]; //起始位置
        [path moveToPoint:CGPointMake(20, 320)];
        [path addLineToPoint:CGPointMake(200, 320)];
        [path stroke];
    }
    
    {// 8.二次贝塞尔曲线
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(0, 360)];
        [path addQuadCurveToPoint:CGPointMake(200, 360)    //终点
                     controlPoint:CGPointMake(100, 320)];  //控制点
        [path stroke];
    }
    
    {// 9. 三次贝塞尔曲线
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(0, 400)];
        [path addCurveToPoint:CGPointMake(300, 400)    //终点
                controlPoint1:CGPointMake(100, 350)    //控制点
                controlPoint2:CGPointMake(200, 450)];  //控制点
        [path stroke];
    }
    
    {// 用 CAShapeLayer 设置边框线颜色
        UIBezierPath *pathA = [UIBezierPath bezierPathWithRect:CGRectMake(100, 480, 100, 100)];
        
        CAShapeLayer *layerA = [[CAShapeLayer alloc] init];
        layerA.lineWidth = 5.f;
        layerA.strokeColor = [UIColor orangeColor].CGColor; //线条填充色
        layerA.fillColor = [UIColor greenColor].CGColor; //背景填充色
        [self.layer addSublayer:layerA];
        
        layerA.path = pathA.CGPath;
    }
    
    {
        // 用 CAShapeLayer 画带阴影的圆
        CGRect frame = CGRectMake(260, 260, 50, 50);
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:frame];
        
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.fillColor = [UIColor greenColor].CGColor;
        shapeLayer.shadowOffset = CGSizeMake(5, 5);
        shapeLayer.shadowColor = [UIColor blackColor].CGColor;
        shapeLayer.shadowOpacity = 0.5;
        [self.layer addSublayer:shapeLayer];
        
        shapeLayer.path = path.CGPath;
    }
    
}

@end
